package com.atguigu.ssyx.activity.service.impl;

import com.atguigu.ssyx.activity.mapper.CouponInfoMapper;
import com.atguigu.ssyx.activity.mapper.CouponRangeMapper;
import com.atguigu.ssyx.activity.service.CouponInfoService;
import com.atguigu.ssyx.client.product.ProductFeignClient;
import com.atguigu.ssyx.enums.CouponRangeType;
import com.atguigu.ssyx.model.activity.CouponInfo;
import com.atguigu.ssyx.model.activity.CouponRange;
import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 杨林
 * @create 2023-09-20 11:07 星期三
 * description:
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {
    
    @Autowired
    private CouponInfoMapper couponInfoMapper;
    
    @Autowired
    private CouponRangeMapper couponRangeMapper;
    
    @Autowired
    private ProductFeignClient productFeignClient;

    /**
     * 分页查询优惠券列表
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage<CouponInfo> selectPage(Long page, Long limit) {
        
        //构建分页对象
        Page<CouponInfo> couponInfoPage = new Page<>(page, limit);
        //构造排序条件
        LambdaQueryWrapper<CouponInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(CouponInfo::getId);
        Page<CouponInfo> pageModel = couponInfoMapper.selectPage(couponInfoPage, wrapper);
        
        pageModel.getRecords().forEach(item -> {
            item.setCouponTypeString(item.getCouponType().getComment());
            if (item.getRangeType() != null){
                item.setRangeTypeString(item.getRangeType().getComment());
            }
        });

        return pageModel;
    }


    /**
     * 根据id获取优惠券
     * @param id
     * @return
     */
    @Override
    public CouponInfo getCouponInfoById(String id) {
        CouponInfo couponInfo = this.getById(id);
        couponInfo.setCouponTypeString(couponInfo.getCouponType().getComment());
        if (couponInfo.getRangeType() != null){
            couponInfo.setRangeTypeString(couponInfo.getRangeType().getComment());
        }
        return couponInfo;
    }


    /**
     * 根据优惠券id获取优惠券规则列表
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findCouponRuleList(Long id) {
        //构建返回结果
        Map<String,Object> result = new HashMap<>();
        //获取优惠券信息
        CouponInfo couponInfo = this.getById(id);
        LambdaQueryWrapper<CouponRange> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CouponRange::getCouponId,id);
        List<CouponRange> couponRanges = couponRangeMapper.selectList(wrapper);
        
        //收集rangeId
        List<Long> rangeIds = 
                couponRanges.stream().map(CouponRange::getCouponId).collect(Collectors.toList());
        
        if (!CollectionUtils.isEmpty(rangeIds)){
            if (couponInfo.getRangeType() == CouponRangeType.SKU){
                List<SkuInfo> skuInfoList = productFeignClient.findSkuInfoList(rangeIds);
                result.put("skuInfoList",skuInfoList);
            }else if (couponInfo.getRangeType() == CouponRangeType.CATEGORY){
                List<Category> categoryList = productFeignClient.findCategoryList(rangeIds);
                result.put("categoryList",categoryList);
            }else {
                //通用
                
            }
        }

        return result;
    }
    
}
